/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MediumManagement;

import Utilities.Functions;
import Utilities.PermissionsConstants;
import org.virtualbox_3_2.MediumVariant;

/**
 * Instances of this class are used by the user interface when a request to
 * clone a hard disk is sent to the media management service.
 *
 * @author Angel Sanadinov
 */
public class CloneDiskRequest extends MediumManagementRequest
{
    private String targetId;        //the id of the target medium
    private MediumVariant variant;  //the medium variant
    private String parentId;        //the id of the parent medium

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public CloneDiskRequest()
    {
        super();
        setRequestedAction(PermissionsConstants.ACTION_MEDIUM_CLONE);
        this.targetId = null;
        this.variant = null;
        this.parentId = null;
    }

    /**
     * Constructs a "clone medium" request with the specified data.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the source medium resides
     * @param mediumId the id of the source medium
     * @param targetId the id of the target medium
     * @param variant image variant which will be created
     * @param parentId the id of the cloned medium's parent (can be <code>null</code>)
     */
    public CloneDiskRequest(int requestorId, int serverId, String mediumId, String targetId, MediumVariant variant, String parentId)
    {
        setRequestedAction(PermissionsConstants.ACTION_MEDIUM_CLONE);
        setRequestorId(requestorId);
        setServerId(serverId);
        setMediumId(mediumId);
        this.targetId = targetId;
        this.variant = variant;
        this.parentId = parentId;
    }

    /**
     * Retrieves the id of the target medium.
     *
     * @return the target medium id
     */
    public String getTargetId()
    {
        return targetId;
    }

    /**
     * Retrieves the medium variant which will be created.
     *
     * @return the medium variant
     */
    public MediumVariant getVariant()
    {
        return variant;
    }

    /**
     * Retrieves the id of the cloned medium's parent.
     *
     * @return the parent medium id, if available or <code>null</code> otherwise
     */
    public String getParentId()
    {
        return parentId;
    }

    /**
     * Sets the id of the target medium.
     *
     * @param targetId the target medium id
     */
    public void setTargetId(String targetId)
    {
        this.targetId = targetId;
    }

    /**
     * Sets the medium variant that will be created.
     *
     * @param variant the medium variant
     */
    public void setVariant(MediumVariant variant)
    {
        this.variant = variant;
    }

    /**
     * Sets the id of the parent of the cloned medium.
     *
     * @param parentId
     */
    public void setParentId(String parentId)
    {
        this.parentId = parentId;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && variant != null && isMediumIdValid()
                && Functions.isUUIDValid(targetId) && (parentId == null || Functions.isUUIDValid(parentId)))
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (mediumId,targetId,variant,parentId)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return CloneDiskRequest.class.getSimpleName() + ": (" + getMediumId()
                + "," + targetId + "," + variant + "," + parentId + ")";
    }
}
